/**
 *====================================================
 * 文件名称: FinanceContractServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.FinanceContractEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: FinanceContractServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(FinanceContractService.class)
public class FinanceContractServiceImpl implements FinanceContractService {

	@Override
	public Long createFinanceContract(FinanceContractEntity entity) {
		entity.setContractUploadDate(DateUtil.todayInt());
		entity.setFinanceContractNumber(SettingConstants.PRE_NUMBER_FINANCE_CONTRACT + RandomUtil.generateTimeNumber());
		PersistUtil.insert(entity);
		return entity.getFinanceContractId();
	}

	@Override
	public void updateFinanceContract(FinanceContractEntity entity) {
		FinanceContractEntity persist = inquireFinanceContractEntity(entity.getFinanceContractId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public FinanceContractEntity inquireFinanceContractEntity(Long financeContractId) {
		return PersistUtil.get(FinanceContractEntity.class, financeContractId);
	}

	@Override
	public FinanceContractEntity inquireFinanceIdContract(Long financeId, Integer contractType) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceContractEntity.class);
		criteria.add(Restrictions.eq("financeId", financeId));
		if (contractType == null) {
			contractType = 2;
		} if (contractType == 2) {
			criteria.add(Restrictions.or(Restrictions.eq("contractType", contractType), Restrictions.isNull("contractType")));
		} else {
			criteria.add(Restrictions.eq("contractType", contractType));
		}
		List<FinanceContractEntity> list = PersistUtil.get(criteria);
		if (list.size() > 1) {
			throw new BusinessException("存在多份的融资合同");
		}
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<Long> inquireFinanceIdList(String financeContractNumber, String financeContractSerialNumber) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceContractEntity.class);
		if (StringUtil.isBlank(financeContractNumber) && StringUtil.isBlank(financeContractSerialNumber)) {
			return null;
		}
		if (StringUtil.isNotBlank(financeContractNumber)) {
			criteria.add(Restrictions.like("financeContractNumber", "%" + financeContractNumber + "%"));
		}
		if (StringUtil.isNotBlank(financeContractSerialNumber)) {
			criteria.add(Restrictions.like("financeContractSerialNumber", "%" + financeContractSerialNumber + "%"));
		}
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

}
